package com.fingard.dsp.bank.directbank.epay01;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.math.BigDecimal;
import java.util.Calendar;

public class EPAY01QryRefund extends EPAY01Base implements IQryRefund {
	@Override
	public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
		RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
		retAts.writeStart();

		String tmpRespCode = "";
		String tmpRespInfo = "";

		int tmpSuccCount = 0;
		BigDecimal tmpTotalAmt = new BigDecimal("0");
		
		tmpRespCode = "-1";
		
		try {
			Calendar tmpCalendarFrom = Calendar.getInstance();
			tmpCalendarFrom.setTime(p_req.getQryStartDate());

			Calendar tmpCalendarTo = Calendar.getInstance();
			tmpCalendarTo.setTime(p_req.getQryEndDate());
			tmpCalendarTo.add(Calendar.DAY_OF_MONTH, 1);

			while (tmpCalendarFrom.compareTo(tmpCalendarTo) == -1) {
				String qryRefundDate = Format.Date8Format.format(tmpCalendarFrom.getTime());
				String localFilePath = StringHelper.trimEnd(bankFront.bkTransFolder, File.separator) + File.separator + p_req.payAct + "_" + qryRefundDate + ".txt";
				WriteBankLogLn("本机交易通知结果文件路径:" + localFilePath);
				File tmpLocalFile = new File(localFilePath);
				if (tmpLocalFile.exists()) {
					tmpRespCode = "0";
					String tmpStrRet = FileHelper.readFileText(localFilePath, getCharset());
					WriteBankLogLn("本机通知结果文件内容:\r\n" + tmpStrRet);
					String[] tmpStrJson = tmpStrRet.split("\r\n");

					for (String s : tmpStrJson) {
						s = "[" + s + "]";
						JSONArray tmpRespArray = JSONArray.parseArray(s);
						for (int i = 0; i < tmpRespArray.size(); i++) {
							JSONObject tmpRespJson = tmpRespArray.getJSONObject(i);
							RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();

							String reqSeqID = tmpRespJson.getString("businessId");
							if (reqSeqID.contains("_")) {
								tmpRetDetail.reqBatchNO = reqSeqID.split("_")[0];
								tmpRetDetail.askNum = reqSeqID.split("_")[1];
							} else {
								tmpRetDetail.askNum = reqSeqID;
							}
							tmpRetDetail.payAct = p_req.payAct;
							tmpRetDetail.recAct = tmpRespJson.getString("cardBookCode");
							tmpRetDetail.amount = new BigDecimal(tmpRespJson.getString("money")).multiply(new BigDecimal("-1")).toString();
							tmpRetDetail.refundTicketDate = Format.Date10Format.format(tmpCalendarFrom.getTime());
							tmpRetDetail.submitTime = Format.DateTimeFormat.format(tmpCalendarFrom.getTime());
							tmpRetDetail.refundTicketReason = tmpRespJson.getString("errorMessage");

							tmpSuccCount++;
							tmpTotalAmt = tmpTotalAmt.add(new BigDecimal(tmpRetDetail.amount));

							retAts.writeRetDetailOnly(tmpRetDetail);
						}
					}
				}
				tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
			}

			retAts.totleAmount = Format.DecimalAmtFormat.format(tmpTotalAmt);
			retAts.totleNum = String.valueOf(tmpSuccCount);
			retAts.writeTotalInfo();
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRespCode = "-1";
			tmpRespInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		 if ("-1".equals(tmpRespCode) && retAts.getWrittenRdCount() == 0) {
			 	tmpRespCode = "0" ;
				retAts.writeRespCode(tmpRespCode);
				retAts.writeRespInfo("当前时间范围内无退票数据");
			}
		 else if ("-1".equals(tmpRespCode)) {
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		}

		retAts.writeEnd();
		return retAts;
	}
}